package com.suse.warmstill.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.suse.warmstill.common.enums.MqQueueConstants;
import com.suse.warmstill.common.model.CallResult;
import com.suse.warmstill.common.model.ListPageModel;
import com.suse.warmstill.entity.ChatMsg;
import com.suse.warmstill.service.ChatMsgService;
import com.suse.warmstill.vo.MessageIndexVo;
import com.suse.warmstill.vo.MessageInfo;
import com.suse.warmstill.vo.MqMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jctools.maps.NonBlockingHashSet;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * @author 黄靖
 * @author 2023-02-12 05:13
 * description:
 **/
@Api(tags = "聊天---》消息查询接口")
@Slf4j
@Controller
public class MessageApi {


    @Resource
    RabbitTemplate rabbitTemplate;

    @Resource
    private ChatMsgService msgService;
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;


    @ApiOperation("首页未读信息接口数")
    @GetMapping("/message/getUnRead/{accepterId}")
    public CallResult<ListPageModel<MessageIndexVo>> getUnRead(
            @PathVariable("accepterId") Long accepterId


    ) {

        //返回 用户列表 id，username，头像，最近的一条信息
        return msgService.getUnRead(accepterId);


    }

    @ApiOperation("查询旧数据，同时进入聊天页面")
    @GetMapping("/message/getHistoryMessage/{acceptId}/{currentPage}/{pageSize}")
    public CallResult<ListPageModel<ChatMsg>> getHistoryMessage(
            @PathVariable("acceptId") Long accepterId,

            @PathVariable("currentPage") int currentPage,
            @PathVariable("pageSize") int pageSize) {
        return msgService.getHistoryMessage(accepterId, currentPage, pageSize);
    }


    // @MessageMapping注解用于映射前端路径，处理消息
    // @DestinationVariable注解，类似SpringMVC的@PathVirable
    // 发送到房间中的信息
    @MessageMapping("/message/sendMessageToFriend/{accepterId}/{senderId}")
    public void sendMessage(@DestinationVariable("accepterId") int accepterId,
                            @DestinationVariable("senderId") int senderId,
                            @RequestBody MessageInfo messageInfo) {
        log.info("messageInfo ===>{}", messageInfo);
        //1、拿到sentid

        String roomId = senderId > accepterId ? String.valueOf(senderId) + String.valueOf(accepterId) : String.valueOf(accepterId) + String.valueOf(senderId);
        //发送数据部分
        simpMessagingTemplate.convertAndSend("/room/message/" + roomId, JSON.toJSONString(messageInfo));
        //数据持久化部分
        final ChatMsg chatMsg = new ChatMsg();
        chatMsg.setMsg(messageInfo.getMessage());
        chatMsg.setSendUserId(String.valueOf(senderId));
        chatMsg.setAcceptUserId(String.valueOf(accepterId));
        chatMsg.setCreateTime(new Date());


        MqMessage mqMessage = new MqMessage(roomId, JSON.toJSONString(chatMsg));
        rabbitTemplate.convertAndSend(MqQueueConstants.CHATSTORE, mqMessage);

    }


}
